﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Word.Editor.Doc
{
    /// <summary>
    /// The Sprm structure specifies a modification to a property of a character, paragraph, table, or section.
    /// </summary>
    public class Sprm
    {
        /// <summary>
        /// An unsigned integer that, when combined with fSpec, specifies the property being modified. 
        /// See the tables in the Single Property Modifiers section (2.6) for the complete list of valid ispmd, fSpec, spra combinations for each sgc.
        /// (9 bits)
        /// </summary>
        public UInt16 ispmd = 0;
        /// <summary>
        /// (1 bit): When combined with ispmd, specifies the property being modified. 
        /// See the tables in the Single Property Modifiers section (2.6) for the complete list of valid ispmd, fSpec, spra combinations for each sgc.
        /// </summary>
        public Byte fSpec = 0;
        /// <summary>
        /// (3 bits): An unsigned integer that specifies the kind of document content to which this Sprm applies. 
        /// The following table specifies the valid values and their meanings.
        /// Sgc Meaning
        /// -----------------------------------------------
        /// |1  |Sprm is modifying a paragraph property.
        /// |2  |Sprm is modifying a character property.
        /// |3  |Sprm is modifying a picture property.
        /// |4  |Sprm is modifying a section property.
        /// |5  |Sprm is modifying a table property.
        /// -----------------------------------------------
        /// </summary>
        public Byte sgc = 0;
        /// <summary>
        /// (3 bits): An unsigned integer that specifies the size of the operand of this Sprm. 
        /// The following table specifies the valid values and their meanings.
        /// 0 - Operand is a ToggleOperand (which is 1 byte in size).
        /// 1   -   Operand is 1 byte.
        /// 2   -   Operand is 2 bytes.
        /// 3   -   Operand is 4 bytes.
        /// 4   -   Operand is 2 bytes.
        /// 5   -   Operand is 2 bytes.
        /// 6   -   Operand is of variable length. The first byte of the operand indicates the size of the rest of the operand, except in the cases of sprmTDefTable and sprmPChgTabs.
        /// 7   -   Operand is 3 bytes.
        /// </summary>
        public Byte spra = 0;
        /// <summary>
        ///  100 - variable length for spra=6;
        /// </summary>
        public int lgOperand = 100;

        public UInt16 valueRead = 0;
        public Sprm(UInt16 newVal)
        {
            valueRead = newVal;
            ispmd = (UInt16)(newVal & 0x01FF);
            fSpec = (Byte)((newVal / 512) & 0x0001);
            sgc = (Byte)((newVal / 1024) & 0x0007);
            spra = (Byte)(newVal / 8192);
            switch (spra) { 
                case 0:
                case 1:
                    lgOperand = 1;
                    break;
                case 2:
                case 4:
                case 5:
                    lgOperand = 2;
                    break;
                case 3:
                    lgOperand = 4;
                    break;
                case 7:
                    lgOperand = 3;
                    break;
            }
        }
    }
}
